﻿@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<html>
<body>
    <h3>服务编辑</h3>
    <form class="layui-form" lay-filter="form" style="margin-left: 100px; margin-right: 100px;">
        <input type="hidden" name="Id" id="hfid" value="@ViewBag.Id" />

        <!-- 第一行：3个字段 -->
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md4">
                <div class="layui-form-item">
                    <label class="layui-form-label">服务名称:</label>
                    <div class="layui-input-block">
                        <input type="text" name="ServiceName" required lay-verify="required|serviceName" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">服务价格:</label>
                    <div class="layui-input-block">
                        <input type="number" name="ServicePrice" required lay-verify="required|number|price" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">起始周:</label>
                        <div class="layui-input-block">
                            <input type="number" name="SerStartTime" required lay-verify="required|number|week" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">服务间隔:</label>
                        <div class="layui-input-block">
                            <input type="number" name="ServiceInterval" lay-verify="number" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">阈值:</label>
                        <div class="layui-input-block">
                            <input type="number" name="ServiceThreshold" lay-verify="number" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">服务次数:</label>
                        <div class="layui-input-block">
                            <input type="number" name="ServiceCount" lay-verify="number" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">服务类型:</label>
                        <div class="layui-input-block">
                            <select name="ServiceType" lay-verify="required">
                                <option value="1">宝宝</option>
                                <option value="2">美容</option>
                                <option value="3" ">护理</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block" style="text-align: center;">
                            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </div>
              </div>
            </div>
            

        <!-- 创建时间显示 -->
        @*<div class="layui-row layui-col-space15">
            <div class="layui-col-md4">
                <div class="layui-form-item">
                    <label class="layui-form-label">创建时间:</label>
                    <div class="layui-input-block">
                        <input type="text" name="CreateTimeDisplay" id="create_time" autocomplete="off" class="layui-input" disabled>
                    </div>
                </div>
            </div>
            <div class="layui-col-md4">
                <div class="layui-form-item">
                    <label class="layui-form-label">最后编辑时间:</label>
                    <div class="layui-input-block">
                        <input type="text" name="EditTimeDisplay" id="edit_time" autocomplete="off" class="layui-input" disabled>
                    </div>
                </div>
            </div>
        </div>*@

        <!-- 提交按钮 -->
        
    </form>

    <script src="~/Assets/bootstrap/js/jquery-1.10.2.min.js"></script>
    <script src="~/Assets/layui/layui.js"></script>
    <script>
        layui.use(['form', 'laydate', 'layer', 'util'], function () {
            var form = layui.form;
            var layer = layui.layer;
            var laydate = layui.laydate;
            var util = layui.util;
            var $ = layui.$;

            // 自定义验证规则
            form.verify({
                serviceName: function (value) {
                    if (value.length < 2) {
                        return '服务名称至少需要2个字符';
                    }
                },
                price: function (value) {
                    if (value <= 0) {
                        return '服务价格必须大于0';
                    }
                },
                week: function (value) {
                    if (value < 1 || value > 52) {
                        return '起始周必须是1-52之间的数字';
                    }
                }
            });

            // 初始化服务数据
            initServiceData();

            // 表单提交
            form.on('submit(demo1)', function (data) {
                submitUpdate(data);
                return false;
            });

            // 初始化服务数据
            function initServiceData() {
                var serviceId = $("#hfid").val();
                if (!serviceId) {
                    layer.msg('未获取到服务ID', { icon: 2 });
                    return;
                }

                var loadIndex = layer.load(1);

                $.get('/ServiceList/GetService', { id: serviceId }, function (res) {
                    layer.close(loadIndex);

                    if (res.code === 0) {
                        // 处理日期显示
                        var createTime = formatDate(res.data.CreateTime);
                        var currentTime = formatDate(new Date());

                        // 填充表单
                        form.val('form', {
                            "ServiceName": res.data.ServiceName,
                            "ServicePrice": res.data.ServicePrice,
                            "SerStartTime": res.data.SerStartTime,
                            "ServiceInterval": res.data.ServiceInterval,
                            "ServiceThreshold": res.data.ServiceThreshold,
                            "ServiceCount": res.data.ServiceCount,
                            "ServiceType": res.data.ServiceType,
                       
                            "Id": res.data.Id
                        });
                    } else {
                        layer.msg(res.msg || '获取服务信息失败', { icon: 2 });
                    }
                }).fail(function () {
                    layer.close(loadIndex);
                    layer.msg('请求失败', { icon: 2 });
                });
            }

            // 提交更新
            function submitUpdate(data) {
                var loadIndex = layer.load(1);

                $.ajax({
                    url: '/ServiceList/DataEdit',
                    type: 'POST',
                    data: data.field,
                    success: function (res) {
                        layer.close(loadIndex);
                        if (res.code === 0) {
                            layer.msg('更新成功', { icon: 1 }, function () {
                                if (parent.layui.table) {
                                    parent.layui.table.reload('testselist');
                                }
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg || '更新失败', { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.close(loadIndex);
                        layer.msg('请求失败', { icon: 2 });
                    }
                });
            }

            // 格式化日期
            function formatDate(date) {
                if (!date) return '';

                if (typeof date === 'string' && date.match(/\/Date\((\d+)\)\//)) {
                    var timestamp = parseInt(date.match(/\/Date\((\d+)\)\//)[1]);
                    date = new Date(timestamp);
                } else if (typeof date === 'string') {
                    date = new Date(date);
                }

                return util.toDateString(date, 'yyyy-MM-dd HH:mm:ss');
            }
        });
    </script>
</body>
</html>